golang struct, map, json 之间的相互转换 |
您所在的位置:网站首页 › golang struct to map › golang struct, map, json 之间的相互转换 |
Golang 的 struct,map,json 互转本文用于记录我在 golang 学习阶段遇到的类型转换问题,针对的是 json 、map、struct 之间相互转换的问题,用到的技术 json 、mapstructure、reflect 三个类库公共代码区域package main
import (
"encoding/json"
"fmt"
"testing"
)
type UserInfoVo struct {
Id string `json:"id"`
UserName string `json:"user_name"`
Address []AddressVo `json:"address"`
}
type AddressVo struct {
Address string `json:"address"`
}
var beforeMap = map[string]interface{}{
"id": "123",
"user_name": "酒窝猪",
"address": []map[string]interface{}{{"address": "address01"}, {"address": "address02"}},
}
var User UserInfoVo
func init() {
User = UserInfoVo{
Id: "01",
UserName: "酒窝猪",
Address: []AddressVo{
{
Address: "湖南",
},
{
Address: "北京",
},
},
}
}一、map, struct 互转1.map 转 struct map 转 struct 有两种方式 1.是通过第三方包 github.com/mitchellh/mapstructure 2.通过 map 转 json,再通过 json 转 struct 第三方包 mapstructure下载依赖,通过第三方依赖进行转换 go get github.com/goinggo/mapstructurefunc TestMapToStructByMod(t *testing.T) { var afterStruct =UserInfoVo{} before := time.Now() err := mapstructure.Decode(beforeMap, &afterStruct) if err!=nil{ fmt.Println(err) } fmt.Printf("result:%+v \n",time.Since(before)) fmt.Printf("result:%+v \n",afterStruct) }result:61.757µs result:{Id:123 UserName: Address:[{Address:address01} {Address:address02}]} --- PASS: TestMapToStructByMod (0.00s) PASS通过 JSON 进行转换先将 map 转换成 JSON,再通过 JSON 转换成 struct 操作有点繁琐 func TestMapToStructByJson(t *testing.T) { beforeMap := map[string]interface {}{ "id":"123", "user_name":"酒窝猪", "address":[]map[string]interface{}{{"address": "address01"}, {"address": "address02"}}, } var afterStruct =UserInfoVo{} before := time.Now() marshal, err := json.Marshal(beforeMap) if err!=nil{ fmt.Println("marshal:",err) return } err = json.Unmarshal(marshal, &afterStruct) if err!=nil{ fmt.Println("unmarshal:",err) return } fmt.Println(time.Since(before)) fmt.Printf("resutlt: %+v",afterStruct) }134.299µs resutlt: {Id:123 UserName:酒窝猪 Address:[{Address:address01} {Address:address02}]}--- PASS: TestMapToStructByJson (0.00s) PASS总结问题: 论性能哪个更佳? 根据结果答案 使用 JSON 需要时间是 134.299µs 使用 mapstructure 需要时间是 61.757µs 结果是使用第三方包 mapstructure 性能更好,那么,是因为什么呢?暂且按下不表 2、struct 转 mapJSON 序列化转换先将 struct 转换成字节数组,再将字节数组转换成 map 打印func TestStructToMapByJson(t *testing.T) { var resultMap interface{} before := time.Now() jsonMarshal, _ := json.Marshal(User) err := json.Unmarshal(jsonMarshal, &resultMap) if err != nil { fmt.Println(err) return } fmt.Println(time.Since(before)) fmt.Printf("%+v",resultMap) }158.857µs map[address:[map[address:湖南] map[address:北京]] id:01 user_name:酒窝猪]--- PASS: TestStructToMapByJson (0.00s) PASS通过反射转换通过反射获取 User 的类型与值func TestStructToMapByReflect(t *testing.T) { var resultMap = make(map[string]interface{},10) before := time.Now() ty:=reflect.TypeOf(User) v:=reflect.ValueOf(User) for i := 0; i < v.NumField(); i++ { resultMap[strings.ToLower(ty.Field(i).Name)]=v.Field(i).Interface() } fmt.Println(time.Since(before)) fmt.Printf("%+v",resultMap) }13.965µs map[address:[{Address:湖南} {Address:北京}] id:01 username:酒窝猪]--- PASS: TestStructToMapByReflect (0.00s) PASS总结问题:论性能哪个更佳? 答案是使用反射的效果更快点,没有那么多繁琐的转换,记住在 make 中进行初始化大小,我试了下,不指定大小与指定大小时间上有 3~4µs 的区别 网络上还有一种方法是使用 structs 包,不过我看了下,该依赖包已经三年没更新了 二、struct, json 互转1. struct 转 jsonfunc TestStructToJsonByJson(t *testing.T) { before := time.Now() marshal, _ := json.Marshal(User) fmt.Println(time.Since(before)) fmt.Printf("%s", marshal) }116.068µs {"id":"01","user_name":"酒窝猪","address":[{"address":"湖南"},{"address":"北京"}]}--- PASS: TestStructToJsonByJson (0.00s) PASS2.json 转 structfunc TestJsonToStructByJson(t *testing.T) { info:=UserInfoVo{} marshal, _ := json.Marshal(User) before := time.Now() json.Unmarshal(marshal,&info) fmt.Println(time.Since(before)) fmt.Printf("%+v",info) }23.009µs {Id:01 UserName:酒窝猪 Address:[{Address:湖南} {Address:北京}]}--- PASS: TestJsonToStructByJson (0.00s) PASS三、map, json 互转1.map 转 jsonfunc TestMapToJson(t *testing.T) { before := time.Now() marshal, _ := json.Marshal(beforeMap) fmt.Println(time.Since(before)) fmt.Printf("%s", marshal) }75.133µs {"address":[{"address":"address01"},{"address":"address02"}],"id":"123","user_name":"酒窝猪"}--- PASS: TestMapToJson (0.00s) PASS2.json 转 mapfunc TestJsonToMap(t *testing.T) { marshal, _ := json.Marshal(beforeMap) resultMap:=make(map[string]interface{},10) before := time.Now() json.Unmarshal(marshal,&resultMap) fmt.Println(time.Since(before)) fmt.Printf("%+v", resultMap) }28.728µs map[address:[map[address:address01] map[address:address02]] id:123 user_name:酒窝猪]--- PASS: TestJsonToMap (0.00s) PASS总结三者之间的转换更多的是关于如果使用 json 内库,只有在 map 转 struct 使用了 mapstructure,struct 转 map 使用了反射,其他转换,更多的是使用 json 内置库进行转换 本文由博客一文多发平台 OpenWrite 发布! |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |